//
//  LFBReadBaseCell.m
//  ONE
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 GroupNine. All rights reserved.
//

#import "LFBReadBaseCell.h"
#import "LFBReadShortModel.h"
#import "LFBReadSerialModel.h"
#import "LFBReadQuestionModel.h"

NSString *const kLFBReadBaseCellID = @"LFBReadBaseCellID";

@interface LFBReadBaseCell()

/** 文章类型图片 */
@property (nonatomic, strong) UIImageView *typeImageView;

/** 文章标题 */
@property (nonatomic, strong) UILabel *titleLabel;

/** 作者 */
@property (nonatomic, strong) UILabel *authorLabel;

/** 内容 */
@property (nonatomic, strong) UILabel *contentLabel;

/** 底部横线 */
@property (nonatomic, strong) UIView *bottomLine;

@end

@implementation LFBReadBaseCell

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self createCell];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createCell];
    }
    return self;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self createCell];
    }
    return self;
}

#pragma mark --创建BaseCell
- (void)createCell
{
    if (_typeImageView) {
        return;
    }
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor = [UIColor clearColor];
    self.contentView.backgroundColor = [UIColor clearColor];
    
//    _typeImageView = ({
//        UIImageView *imageView = [UIImageView new];
//        imageView.backgroundColor = [UIColor whiteColor];
//        [self.contentView addSubview:imageView];
//        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.size.sizeOffset(CGSizeMake(41, 19));
//            make.top.equalTo(self.contentView).offset(16);
//            make.right.equalTo(self.contentView).offset(-10);
//        }];
//        
//        imageView;
//    });
    // 初始化文章类型图片
    _typeImageView = [UIImageView new];
    _typeImageView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_typeImageView];
    [_typeImageView makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake(41, 19));
        make.top.equalTo(self.contentView).offset(16);
        make.right.equalTo(self.contentView).offset(-10);
    }];
    
    // 初始化文章标题Label
    _titleLabel = [UILabel new];
    _titleLabel.textColor = LYLDarkBlackTextColor;
    _titleLabel.font = BoldFontWithSize(18);
    _titleLabel.numberOfLines = 0;
    [self.contentView addSubview:_titleLabel];
    [_titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(10);
        make.top.equalTo(_typeImageView);
        make.right.lessThanOrEqualTo(_typeImageView.left).offset(-10);
    }];
    
    
    // 初始化作者Label
    _authorLabel = [UILabel new];
    _authorLabel.textColor = LYLLightBlackTextColor;
    _authorLabel.font = FontWithSize(13);
    [self.contentView addSubview:_authorLabel];
    [_authorLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel.bottom).offset(10);
        make.left.right.equalTo(_titleLabel);
    }];
    
    // 初始化内容Label
    _contentLabel = [UILabel new];
    _contentLabel.textColor = LYLLightBlackTextColor;
    _contentLabel.font = FontWithSize(13);
    _contentLabel.numberOfLines = 0;
    [self.contentView addSubview:_contentLabel];
    [_contentLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_authorLabel.bottom).offset(10);
        make.left.right.equalTo(_authorLabel);
        make.bottom.equalTo(self.contentView).offset(-24);
    }];
    
    // 初始化分割线
    _bottomLine = [UIView new];
    _bottomLine.backgroundColor = LYLSeparatorColor;
    [self.contentView addSubview:_bottomLine];
    [_bottomLine makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(0.5);
        make.left.bottom.right.equalTo(self.contentView).insets(UIEdgeInsetsMake(0, 10, 0, 10));
    }];
}

// 设置BaseCell
- (void)configureCellWithBaseModel:(BaseModel *)model
{
    if ([model isMemberOfClass:[LFBReadShortModel class]]) {
        [self configureCellWithReadShortModel:(LFBReadShortModel *)model];
    }
    else if ([model isMemberOfClass:[LFBReadSerialModel class]])
    {
        [self configureCellWithReadSerialModel:(LFBReadSerialModel *)model];
    }
    else if ([model isMemberOfClass:[LFBReadQuestionModel class]])
    {
        [self configureCellWithReadQuestionModel:(LFBReadQuestionModel *)model];
    }
}

// 设置短篇cell
- (void)configureCellWithReadShortModel:(LFBReadShortModel *)model
{
    _typeImageView.image = [UIImage imageNamed:@"icon_read"];
    _titleLabel.text = model.hp_title;
    
    if (model.author.count > 0) {
        LYLAuthorItemModel *authorModel = model.author[0];
        _authorLabel.text = authorModel.user_name;
    }
    else
    {
        _authorLabel.text = @"";
    }
    
    [self commonConfigureContentLabelWithText:model.guide_word];
    _bottomLine.hidden = NO;
}

// 设置连载cell
- (void)configureCellWithReadSerialModel:(LFBReadSerialModel *)model
{
    _typeImageView.image = [UIImage imageNamed:@"icon_serial"];
    
    _titleLabel.text = model.title;
    _authorLabel.text = model.model.user_name;
    [self commonConfigureContentLabelWithText:model.excerpt];
    _bottomLine.hidden = NO;
}

// 设置问答cell
- (void)configureCellWithReadQuestionModel:(LFBReadQuestionModel *)model
{
    _typeImageView.image = [UIImage imageNamed:@"icon_question"];
    _titleLabel.text = model.question_title;
    _authorLabel.text = model.answer_title;
    [self commonConfigureContentLabelWithText:model.answer_content];
    _bottomLine.hidden = YES;
}

// 设置共同的内容label
- (void)commonConfigureContentLabelWithText:(NSString *)content
{
    if (IsStringEmpty(content)) {
        return;
    }
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 10;
    NSDictionary *attrsDictionary = @{NSFontAttributeName : _contentLabel.font, NSForegroundColorAttributeName : _contentLabel.textColor, NSParagraphStyleAttributeName : paragraphStyle};
    
    NSAttributedString *attributedString =  [[NSAttributedString alloc] initWithString:content attributes:attrsDictionary];
    _contentLabel.attributedText = attributedString;
}

@end
